// 这道题很棒, 因为考察了二叉树很多知识点
// 首先是构造二叉树
// 其次是二叉树中序遍历
// 其次是二叉树层序遍历
const readline = require("readline");
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

class TreeNode {
    constructor(value, left = null, right = null) {
        this.value = value
        this.left = left
        this.right = right
    }
}

let initArrA = [];
let path;
let initArrB = [];
rl.on("line", (line) => {
    if (initArrA.length === 0) {
        initArrA = JSON.parse(line.trim().replaceAll(/\s+/g, ""))
    } else if (path === undefined) {
        path = line.trim().replaceAll("\s+", "")
    } else if (initArrB.length === 0) {
        initArrB = JSON.parse(line.trim().replaceAll(/\s+/g, ""))
        getResult(initArrA, initArrB, path)

        // clear
        initArrA.length = 0
        path.length = 0
        initArrB.length = 0
    }

})

function getResult(initArrA, initArrB, path) {
    // 首先是构造二叉树
    let indexA = 0
    let rootA = new TreeNode(initArrA[0])
    let pqA = [rootA]
    while (pqA.length) {
        let node = pqA.shift()
        indexA++
        if (indexA < initArrA.length) {
            let left = new TreeNode(initArrA[indexA])
            node.left = left
            pqA.push(left)
        }
        indexA++
        if (indexA < initArrA.length) {
            let right = new TreeNode(initArrA[indexA])
            node.right = right
            pqA.push(right)
        }
    }

    let indexB = 0
    let rootB = new TreeNode(initArrB[0])
    let pqB = [rootB]
    while (pqB.length) {
        let node = pqB.shift()
        indexB++
        if (indexB < initArrB.length) {
            let left = new TreeNode(initArrB[indexB])
            node.left = left
            pqB.push(left)
        }
        indexB++
        if (indexB < initArrB.length) {
            let right = new TreeNode(initArrB[indexB])
            node.right = right
            pqB.push(right)
        }
    }

// 其次是二叉树中序遍历
    let ans = {parent: null, direction: null} // parentNode, "left" {parent, direction}
    dfs(rootA, `/${rootA.value}`, path, ans)
    ans.parent[ans.direction] = rootB

// 其次是二叉树层序遍历
    let pq = [rootA]
    let result = []
    while (pq.length) {
        let node = pq.shift()
        if (node !== null && node.value !== 0) {
            result.push(node.value)
            pq.push(node.left)
            pq.push(node.right)
        }
    }
    console.log(JSON.stringify(result),)
}

function dfs(root, currentPath, path, ans) {
    if (ans.parent) {
        return
    }
    if (root.left !== null) {
        let temp = currentPath + "/" + root.left.value
        if (temp === path) {
            ans.parent = root
            ans.direction = "left"
            return
        } else {
            dfs(root.left, temp, path, ans)
        }

    }
    if (root.right !== null) {
        let temp = currentPath + "/" + root.right.value
        if (temp === path) {
            ans.parent = root
            ans.direction = "right"
            return
        } else {
            dfs(root.right, temp, path, ans)
        }
    }
}